/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * author Svetlana Shikhutskaya, 
 *        Ivchenko Oleg
 */
var status;
var warehouse;
var user;
var orders;
var count = 0;
var pageNumber = 1;
var oldcount = 0;
var start_date;
var end_date;
var totalNumber;

function init() {
    loadOrders(false);
}

$(function() { //for export to Exscel
    $(document).on('click', '.export-exel', function(e) {
        status = document.getElementById("select-status").value;
        warehouse = document.getElementById("select-warehouse").value;
        user = document.getElementById("select-user").value;
        start_date = document.getElementById("datepickerStart").value;
        end_date = document.getElementById("datepickerEnd").value;
        e.preventDefault();
        $.post('ReportBuildingNonStatistic', {
            status: status,
            warehouse: warehouse,
            user: user,
            start_date: start_date,
            end_date: end_date
        }, function(result) {
            })
    })
})

function loadOrders(paging) {
    status = document.getElementById("select-status").value;
    warehouse = document.getElementById("select-warehouse").value;
    user = document.getElementById("select-user").value;
    start_date = document.getElementById("datepickerStart").value;
    end_date = document.getElementById("datepickerEnd").value;
    $.post('ReportInJspAllOrders',
    {
        page: pageNumber,
        status: status,
        warehouse: warehouse,
        user: user,
        start_date: start_date,
        end_date: end_date
    },
    function(result) {
        orders = $.parseJSON(result);
        if(orders.length === 0){
            document.getElementById("repGen").disabled = true;
        //            deleteRows();
        //            drawRowsIfNull();
        }
        else{
            document.getElementById("repGen").disabled = false;
        }
        if (count > 0) {
            oldcount = count;
        }
        count = orders.length;
        if (count > 0) {
            deleteRows();
            drawRows();
        }
        else {
            if (count === 0 && !paging) {
                deleteRows();
            } else {
                if (pageNumber !== 1) {
                    pageNumber--;
                }
            }
        }
    })
    document.getElementById("num").innerHTML=pageNumber;
}

function drawRows() {
    for (var i = 0; i < orders.length; i++) {
        $('.table-orders').append('<tr id=row-' + i + '></tr>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="order-id-' + i + '">' + orders[i].trackingNumber + '</span></td>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="date-' + i + '">' + orders[i].date + '</span></td>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="status-' + i + '">' + orders[i].status + '</span></td>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="warehouse-' + i + '">' + orders[i].warehouse + '</span></td>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="user-' + i + '">' + orders[i].user + '</span></td>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="address-' + i + '">' + orders[i].address + '</span></td>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="sh-price-' + i + '">' + orders[i].shippingPrice + '</span></td>');
        $('.table-orders > tbody > tr:last').append('<td class="str"><span id="pr-price-' + i + '">' + orders[i].productPrice + '</span></td>');
    }
}

function drawRowsIfNull(){
    $('.table-orders').append('<tr id=row-1></tr>');
    $('.table-orders > tbody > tr:last').append('<td colspan="8" class="str"><span id="order-id-1">No orders found!</span></td>');
}

function deleteRows() {
    for (var i = 0; i < oldcount; i++) {
        $('#row-' + i).remove();
    }
    if(oldcount === 0){
        $('#row-1').remove();
    }
}

$(function() {
    $(document).on('click', '.left-page', function(e) {
        e.preventDefault();
        temp = false;
        if (pageNumber !== 1) {
            pageNumber--;
            loadOrders(true);
        }
    })
})

$(function() {
    $(document).on('click', '.right-page', function(e) {
        e.preventDefault();
        temp = true;
        pageNumber++;
        loadOrders(true);
    })
})